<?php
error_reporting(4);
require_once("functions.php");
//$old_error_handler = set_error_handler("myErrorHandler");

set_time_limit(0);

define('DEBUG',false);
define("FSOCK_ENABLED", true);

// Strip out javascript, styles etc..
$GLOBALS['search_regexp'] =  array ('@<script[^>]*?>.*?</script>@si', // Strip out javascript
				 '@<a[^>]*?>.*?</a>@si', // Strip out A links
				 '@<input[^>]*?>.*?</input>@si', // Strip out A links
				 '@<select[^>]*?>.*?</select>@si', // Strip out A links
				 '@<textarea[^>]*?>.*?</textarea>@si', // Strip out A links
				 '@<a[^>]*?>.*?</a>@si', // Strip out A links
				 '|',
                 '@<[\/\!]*?[^<>]*?>@si',          // Strip out HTML tags
                 '@([\r\n])[\s]+@',                // Strip out white space
                 '@&(quot|#34);@i',                // Replace HTML entities
                 '@&(amp|#38);@i',
                 '@&(lt|#60);@i',
                 '@&(gt|#62);@i',
                 '@&(nbsp|#160);@i',
                 '@&(iexcl|#161);@i',
                 '@&(cent|#162);@i',
                 '@&(pound|#163);@i',
                 '@&(copy|#169);@i',
                 '@&#(\d+);@e'
				 );                    // evaluate as php            
$GLOBALS['replace_regexp'] = array (
				  '',
                  '',
				  '',
				  '',
				  '',
				  '',
				  ' ',
                  '',
                  ' ',
                  '',
                  '',
                  '',
                  '',
                  '',
                  '',
                  '',
                  '',
                  '');

require_once("functions.php");
require_once("urlreader.php");
require_once("linkchecker.php");

$allow_url_fopen = ini_get('allow_url_fopen');
if (!$allow_url_fopen) {
	ini_set('allow_url_fopen', 1);
}

$allow_url_fopen = ini_get('allow_url_fopen');
if (!$allow_url_fopen && !FSOCK_ENABLED ) {
	echo '<span class="error"><b>Note</b>: Please enable \'<em>allow_url_fopen</em>\' option in PHP configuration file</span><br/>';
}

$pageurl = trim(@$_POST['pageurl']);

require_once("header.php");
$MY_LINKS = array();

?>
<TABLE cellSpacing="0" cellPadding="1" align="center" border="0">
  <TBODY>
    <TR>
      <TD bgColor="#3366cc"><TABLE cellSpacing="0" cellPadding="5" align="center" class="headingbar" border="0">
        <TBODY>
          <TR>
            <TD align="middle"><strong>Spider Viewer</strong> </TD>
          </TR>
          <TR>
            <TD align="middle" bgColor="#ffffff"><FORM action="spider_test.php" method="post">
              <INPUT type="hidden" value="1" name="c"><span class="bodytext">
              Enter url   ( eg: <strong><em>http://www.website.com/page.html</em></strong>):</span> <BR>
              <INPUT size="50" value="<?php echo (empty($pageurl)?'http://':$pageurl); ?>" name="pageurl">
              <BR>
              <BR>
              <INPUT name="submit" type="submit" value="Go" class="button">
            </FORM></TD>
          </TR>
        </TBODY>
      </TABLE></TD>
    </TR>
  </TBODY>
</TABLE>
<br>
<?php
if (!empty($pageurl) ) {

	$link_check = new LinkChecker($pageurl);
	$BASE_URL = $link_check->get_base();
	$pageurl = $link_check->get_full_url($pageurl);
	$short_url = $link_check->remove_base($pageurl);
	$robots = get_robots($BASE_URL);
	
	$reader = new URLReader();
	
	list($headers, $htmlCode) = $reader->read_data($pageurl, $short_url);
	$status = parse_response_status($headers[0]);
	if (!is_good_status($status) ){
		$htmlCode = '';
	}
	
	$size = strlen($htmlCode);
	//$text = preg_replace($GLOBALS['search_regexp'], $GLOBALS['replace_regexp'], $htmlCode);
	$body = get_html_tags($htmlCode, 'body');
	$text = preg_replace($GLOBALS['search_regexp'], $GLOBALS['replace_regexp'], $body[0]['text']);
	//print_r($text); die;
	//$text = strip_tags($text);
	//$text = html_entity_decode($text);
	//$text = str_replace("\n"," ",$text);
	$text = preg_replace("/[^a-zA-Z\s\r\n\t]/", ' ', $text);
	//print_r($text); die;

	$words = str_word_count($text, 1);
	foreach ($words as $k=>$v) {
		$distinct_words[$v] = $k;
	}
	
	$anchors = get_html_tags($htmlCode, 'a');
	$link_check->get_links($anchors);
	
	$titles = get_html_tags($htmlCode, 'title');
	//echo "<pre>"; print_r($titles); die;
	$title = @$titles[0]['text'];

	$metas = get_html_tags($htmlCode, 'meta', false);
	//echo "<pre>"; print_r($metas); die;
	
	$keywords = '';
	$description = '';
	foreach ($metas as $meta) {
		$attribs = $meta['attributes'];
		if (@$attribs['name'] == 'keywords') {
			$keywords = $attribs['content'];
		}
		if (@$attribs['name'] == 'description') {
			$description = $attribs['content'];
		}
	}
	
	$display_url = $link_check->get_url_with_base($BASE_URL, $pageurl);
?>
<TABLE cellSpacing="2" cellPadding="1" width="98%" align="center" border="0">
  <TBODY>
    <TR class="tr1" height="25">
      <TD width="230">URL:</TD>
      <TD><a href="<?php echo $display_url; ?>"><?php echo $display_url; ?></a></TD>
    </TR>
    <TR class="tr2" height="25">
      <TD>Title:</TD>
      <TD><?php echo $title; ?></TD>
    </TR>
    <TR class="tr1" height="25">
      <TD>Description:</TD>
      <TD><?php echo $description; ?></TD>
    </TR>
    <TR class="tr2" height="25">
      <TD>Keywords:</TD>
      <TD><?php echo $keywords; ?></TD>
    </TR>
    <TR class="tr1" height="25">
      <TD>Size:</TD>
      <TD><?php echo $size; ?></TD>
    </TR>
    <TR class="tr2" height="25">
      <TD>Text:</TD>
      <TD><?php echo $text; ?></TD>
    </TR>
    <TR class="tr1" height="25">
      <TD>Number of words:</TD>
      <TD><?php echo count($words); ?></TD>
    </TR>
    <TR class="tr2" height="25">
      <TD>Number of distinct words:</TD>
      <TD><?php echo count($distinct_words); ?></TD>
    </TR>
    <TR class="tr1" height="25">
      <TD vAlign="top">Links:</TD>
      <TD><?php 
		//Check dups
		$dups = array();
			foreach($MY_LINKS as $link) {
				if (!in_array(strtolower(trim($link)), $dups) ) {
					echo '<a href="'.$link.'">'.$link.'</a><br>';
					$dups[] = strtolower(trim($link));
				}
			}
	   ?></TD>
    </TR>
    <TR class="tr2" height="25">
      <TD vAlign="top">HTML: </TD>
      <TD></TD>
    </TR>
	<TR class="tr2">
      <TD colspan="2" align="center" valign="top"><TEXTAREA name="textarea" cols="80" rows="30"><?php echo htmlentities($htmlCode); ?></TEXTAREA></TD>
    </TR>
  </TBODY>
</TABLE>
<BR>
<?php
}
require_once("footer.php");
?>